<!-- src/components/Layout.vue -->
<template>
  <el-container style="height: 100vh;">
    <!-- 顶部导航栏 -->
    <el-header class="header">
      <div class="logo">
        <!-- 可选的 logo 图片 -->
        <img src="@/assets/car.png" alt="Logo" class="logo-image" />
        <span class="title">无人车智能控制系统</span>
      </div>
      <el-menu
        class="el-menu-demo"
        mode="horizontal"
        @select="handleSelect"
        background-color="#ffffff"
        text-color="#333333"
        active-text-color="#409EFF"
        :default-active="activeMenu"
      >
        <el-menu-item index="Dashboard">主页</el-menu-item>
        <el-menu-item index="UserManagement">用户管理</el-menu-item>
        <el-menu-item index="RealTimeControl">实时控制</el-menu-item>
        <el-menu-item index="AutoInspection">自动巡检</el-menu-item>
        <el-menu-item index="Logout" class="logout-button">退出登录</el-menu-item>
      </el-menu>
    </el-header>

    <!-- 主体内容 -->
    <el-main>
      <router-view />
    </el-main>
  </el-container>
</template>

<script>
import { ref, onMounted } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { ElNotification } from 'element-plus';

export default {
  name: 'Layout',
  setup() {
    const router = useRouter();
    const route = useRoute();
    const activeMenu = ref(route.name || 'Dashboard');

    // 监控路由变化以更新活动菜单项
    onMounted(() => {
      activeMenu.value = route.name;
    });

    const handleSelect = (key) => {
      if (key === 'Logout') {
        logout();
      } else {
        // 根据菜单项名称导航到对应路由
        switch (key) {
          case 'Dashboard':
            router.push({ name: 'Dashboard' });
            break;
          case 'UserManagement':
            router.push({ name: 'UserManagement' }); // 确保在路由中定义了对应的路由名称
            break;
          case 'RealTimeControl':
            router.push({ name: 'RealTimeControl' }); // 确保在路由中定义了对应的路由名称
            break;
          case 'AutoInspection':
            router.push({ name: 'AutoInspection' }); // 确保在路由中定义了对应的路由名称
            break;
          default:
            router.push({ name: 'Dashboard' });
        }
      }
    };

    const logout = () => {
      localStorage.removeItem('authToken');
      ElNotification.success({
        title: '成功',
        message: '退出登录成功',
        type: 'success',
      });
      router.push({ name: 'Login' });
    };

    return {
      activeMenu,
      handleSelect,
      logout,
    };
  },
};
</script>

<style scoped>
.header {
  display: flex;
  align-items: center;
  justify-content: center; /* 让整个导航居中 */
  background-color: #ffffff;
  border-bottom: 1px solid #eaeaea;
  padding: 0 20px;
}

.logo {
  position: absolute; /* 固定 logo 的位置 */
  left: 20px;
  display: flex;
  align-items: center;
}

.logo-image {
  width: 40px;
  margin-right: 10px;
}

.title {
  font-size: 20px;
  font-weight: bold;
  color: #42b983;
}

.el-menu-demo {
  flex: 1; /* 占据剩余空间 */
  display: flex;
  justify-content: center; /* 居中导航菜单 */
  background-color: transparent; /* 确保背景透明，和 header 的背景一致 */
}

.logout-button {
  position: absolute;
  right: 20px; /* 固定退出按钮的位置 */
}
</style>
